{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data Summarization - Construction permits, part 2/2\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Aggregate-points\" data-toc-modified-id=\"Aggregate-points-1\">Aggregate points</a></span></li><li><span><a href=\"#Aggregation-results\" data-toc-modified-id=\"Aggregation-results-2\">Aggregation results</a></span></li><li><span><a href=\"#Enrich-the-data\" data-toc-modified-id=\"Enrich-the-data-3\">Enrich the data</a></span></li><li><span><a href=\"#Share-your-work\" data-toc-modified-id=\"Share-your-work-4\">Share your work</a></span></li></ul></div>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the \"Data Visualization - Construction permits, part 1/2\" notebook, we explored your data and learned a little about the spatial and temporal trends of permit activity in Montgomery County. In this lesson, we'll move beyond exploration and run spatial analysis tools to answer specific questions that can't be answered by the data itself. In particular, we want to know why permits spiked in Germantown in 2011 and predict where future permit spikes - and, by extension, future growth - are likely to occur.\n",
    "\n",
    "First, we'll aggregate the points by ZIP Code. We'll enrich each ZIP Code with demographic information and learn more about the demographic conditions that led to such rapid growth in such a short time. Once you determine why growth occurred where and when it did, we'll locate other ZIP Codes with similar demographic characteristics to predict future growth.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Aggregate points\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime as dt\n",
    "\n",
    "import pandas as pd\n",
    "from arcgis.features.enrich_data import enrich_layer\n",
    "from arcgis.features.summarize_data import aggregate_points\n",
    "from arcgis.gis import GIS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "gis = GIS(\"home\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://notebooks.maps.arcgis.com/home/item.html?id=854958bac3004f4c88aaca9f64a4d2d9' target='_blank'>\n",
       "                        <img src='' width='200' height='133' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://notebooks.maps.arcgis.com/home/item.html?id=854958bac3004f4c88aaca9f64a4d2d9' target='_blank'><b>Commercial_Permits_since_2010</b>\n",
       "                        </a>\n",
       "                        <br/>test data<br/><img src='https://notebooks.maps.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\" width=16 height=16>Feature Layer Collection by api_data_owner\n",
       "                        <br/>Last Modified: June 30, 2019\n",
       "                        <br/>0 comments, 959 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"Commercial_Permits_since_2010\" type:Feature Layer Collection owner:api_data_owner>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "permits = gis.content.search(\n",
    "    \"Commercial_Permits_since_2010 owner:api_data_owner\",\n",
    "    item_type=\"Feature layer\",\n",
    "    outside_org=True)[0]\n",
    "permits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "permits_layer = permits.layers[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use authoritative data from Living Atlas to assign a variable to the item.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://notebooks.maps.arcgis.com/home/item.html?id=d6f7ee6129e241cc9b6f75978e47128b' target='_blank'>\n",
       "                        <img src='' width='200' height='133' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://notebooks.maps.arcgis.com/home/item.html?id=d6f7ee6129e241cc9b6f75978e47128b' target='_blank'><b>USA ZIP Code Boundaries</b>\n",
       "                        </a>\n",
       "                        <br/>U.S. ZIP Code Boundaries provides ZIP Code, postal district name, population, and area for the ZIP Code areas in the United States.<br/><img src='https://notebooks.maps.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\" width=16 height=16>Feature Layer Collection by esri_dm\n",
       "                        <br/>Last Modified: August 19, 2024\n",
       "                        <br/>0 comments, 216691610 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"USA ZIP Code Boundaries\" type:Feature Layer Collection owner:esri_dm>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zip_boundaries = gis.content.search(\n",
    "    \"title:'USA ZIP Code Boundaries'\",\n",
    "    item_type=\"Feature layer\",\n",
    "    outside_org=True,\n",
    ")[0]\n",
    "zip_boundaries"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since the item is a feature layer collection, using the layers property will give us a list of layers.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "zip_layer = zip_boundaries.layers[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, you'll use this layer to aggregate permit points. By default, the parameters are set to use the ZIP Codes as the area layer, the permits as the layer to be aggregated, and the layer style to be based on permit count. These parameters are exactly what you want.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "{\"cost\": 43.492}\n"
     ]
    }
   ],
   "source": [
    "permits_agg_by_zip = aggregate_points(\n",
    "    permits_layer,\n",
    "    zip_layer,\n",
    "    keep_boundaries_with_no_points=False,\n",
    "    output_name=f\"permits_aggregated_by_zip_{dt.now().strftime('%Y%m%d%H%M%S')}\"\n",
    ")\n",
    "permits_agg_layer = permits_agg_by_zip.layers[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Aggregation results\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"\" alt=\"Image\" />"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "map_agg = gis.map(\"Montgomery County, Maryland\")\n",
    "map_agg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "map_agg.content.add(permits_agg_by_zip)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The new layer looks like a point layer, but it's actually a polygon layer with a point symbology. Each point represents the number of permits per ZIP Code area. Larger points indicate ZIP Codes with more permits.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "permits_agg_sdf = pd.DataFrame.spatial.from_layer(permits_agg_layer)\n",
    "permits_agg_sdf.columns = [x if x == \"SHAPE\" else x.lower()\n",
    "                           for x in permits_agg_sdf.columns]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>objectid</th>\n",
       "      <th>zip_code</th>\n",
       "      <th>po_name</th>\n",
       "      <th>state</th>\n",
       "      <th>population</th>\n",
       "      <th>pop_sqmi</th>\n",
       "      <th>sqmi</th>\n",
       "      <th>point_count</th>\n",
       "      <th>analysisarea</th>\n",
       "      <th>SHAPE</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>20012</td>\n",
       "      <td>Washington</td>\n",
       "      <td>DC</td>\n",
       "      <td>16069</td>\n",
       "      <td>6808.9</td>\n",
       "      <td>2.36</td>\n",
       "      <td>6</td>\n",
       "      <td>2.356958</td>\n",
       "      <td>{\"rings\": [[[-8577435.61330738, 4719746.347215...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>20783</td>\n",
       "      <td>Hyattsville</td>\n",
       "      <td>MD</td>\n",
       "      <td>50387</td>\n",
       "      <td>8061.92</td>\n",
       "      <td>6.25</td>\n",
       "      <td>1</td>\n",
       "      <td>6.252594</td>\n",
       "      <td>{\"rings\": [[[-8571874.52570738, 4716758.150415...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>20814</td>\n",
       "      <td>Bethesda</td>\n",
       "      <td>MD</td>\n",
       "      <td>33669</td>\n",
       "      <td>6588.85</td>\n",
       "      <td>5.11</td>\n",
       "      <td>1143</td>\n",
       "      <td>5.109396</td>\n",
       "      <td>{\"rings\": [[[-8586356.31200737, 4725395.745915...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>20815</td>\n",
       "      <td>Chevy Chase</td>\n",
       "      <td>MD</td>\n",
       "      <td>30893</td>\n",
       "      <td>5774.39</td>\n",
       "      <td>5.35</td>\n",
       "      <td>584</td>\n",
       "      <td>5.354012</td>\n",
       "      <td>{\"rings\": [[[-8584098.68600737, 4717919.800115...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>20816</td>\n",
       "      <td>Bethesda</td>\n",
       "      <td>MD</td>\n",
       "      <td>16512</td>\n",
       "      <td>3581.78</td>\n",
       "      <td>4.61</td>\n",
       "      <td>155</td>\n",
       "      <td>4.608462</td>\n",
       "      <td>{\"rings\": [[[-8591590.85510738, 4717142.631815...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>20817</td>\n",
       "      <td>Bethesda</td>\n",
       "      <td>MD</td>\n",
       "      <td>39114</td>\n",
       "      <td>2815.98</td>\n",
       "      <td>13.89</td>\n",
       "      <td>734</td>\n",
       "      <td>13.891985</td>\n",
       "      <td>{\"rings\": [[[-8595385.97030737, 4718600.340215...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>20818</td>\n",
       "      <td>Cabin John</td>\n",
       "      <td>MD</td>\n",
       "      <td>1882</td>\n",
       "      <td>1920.41</td>\n",
       "      <td>0.98</td>\n",
       "      <td>13</td>\n",
       "      <td>0.980814</td>\n",
       "      <td>{\"rings\": [[[-8591561.56690738, 4717346.303815...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>20832</td>\n",
       "      <td>Olney</td>\n",
       "      <td>MD</td>\n",
       "      <td>26083</td>\n",
       "      <td>2768.9</td>\n",
       "      <td>9.42</td>\n",
       "      <td>212</td>\n",
       "      <td>9.424987</td>\n",
       "      <td>{\"rings\": [[[-8584094.06620738, 4748044.991815...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9</td>\n",
       "      <td>20833</td>\n",
       "      <td>Brookeville</td>\n",
       "      <td>MD</td>\n",
       "      <td>7944</td>\n",
       "      <td>347.35</td>\n",
       "      <td>22.87</td>\n",
       "      <td>40</td>\n",
       "      <td>22.868169</td>\n",
       "      <td>{\"rings\": [[[-8584384.49880738, 4760043.003015...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>20837</td>\n",
       "      <td>Poolesville</td>\n",
       "      <td>MD</td>\n",
       "      <td>6890</td>\n",
       "      <td>163.66</td>\n",
       "      <td>42.1</td>\n",
       "      <td>51</td>\n",
       "      <td>42.101676</td>\n",
       "      <td>{\"rings\": [[[-8627670.41480738, 4738184.291215...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   objectid zip_code      po_name state  population  pop_sqmi   sqmi  \\\n",
       "0         1    20012   Washington    DC       16069    6808.9   2.36   \n",
       "1         2    20783  Hyattsville    MD       50387   8061.92   6.25   \n",
       "2         3    20814     Bethesda    MD       33669   6588.85   5.11   \n",
       "3         4    20815  Chevy Chase    MD       30893   5774.39   5.35   \n",
       "4         5    20816     Bethesda    MD       16512   3581.78   4.61   \n",
       "5         6    20817     Bethesda    MD       39114   2815.98  13.89   \n",
       "6         7    20818   Cabin John    MD        1882   1920.41   0.98   \n",
       "7         8    20832        Olney    MD       26083    2768.9   9.42   \n",
       "8         9    20833  Brookeville    MD        7944    347.35  22.87   \n",
       "9        10    20837  Poolesville    MD        6890    163.66   42.1   \n",
       "\n",
       "   point_count  analysisarea  \\\n",
       "0            6      2.356958   \n",
       "1            1      6.252594   \n",
       "2         1143      5.109396   \n",
       "3          584      5.354012   \n",
       "4          155      4.608462   \n",
       "5          734     13.891985   \n",
       "6           13      0.980814   \n",
       "7          212      9.424987   \n",
       "8           40     22.868169   \n",
       "9           51     42.101676   \n",
       "\n",
       "                                               SHAPE  \n",
       "0  {\"rings\": [[[-8577435.61330738, 4719746.347215...  \n",
       "1  {\"rings\": [[[-8571874.52570738, 4716758.150415...  \n",
       "2  {\"rings\": [[[-8586356.31200737, 4725395.745915...  \n",
       "3  {\"rings\": [[[-8584098.68600737, 4717919.800115...  \n",
       "4  {\"rings\": [[[-8591590.85510738, 4717142.631815...  \n",
       "5  {\"rings\": [[[-8595385.97030737, 4718600.340215...  \n",
       "6  {\"rings\": [[[-8591561.56690738, 4717346.303815...  \n",
       "7  {\"rings\": [[[-8584094.06620738, 4748044.991815...  \n",
       "8  {\"rings\": [[[-8584384.49880738, 4760043.003015...  \n",
       "9  {\"rings\": [[[-8627670.41480738, 4738184.291215...  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "permits_agg_sdf.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Review some basic statistics about the data.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count          45.0\n",
       "mean     249.422222\n",
       "std      303.014438\n",
       "min             1.0\n",
       "25%            27.0\n",
       "50%           137.0\n",
       "75%           316.0\n",
       "max          1143.0\n",
       "Name: point_count, dtype: Float64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "permits_agg_sdf[\"point_count\"].describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Although most of the large point symbols on the map are in the southeast corner, near Washington, D.C., there are a few large points in the northwest. In particular, there is a very large circle in the ZIP Code located in Clarksburg. (If you're using different ZIP Code data, this area may be identified as ZIP Code 20871 instead.) The ZIP code has 948 permits. Additionally, this area geographically corresponds to the hot spot you identified in the previous lesson. This ZIP Code is one that you'll focus on when you enrich your layer with demographic data.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Enrich the data\n",
    "\n",
    "Are there demographic characteristics about the Clarksburg ZIP Code that contributed to its high growth? If so, are there other areas with those characteristics that may experience growth in the future? To answer these questions, you'll use the **Enrich Data** analysis tool. This tool adds demographic attributes of your choice to your data. Specifically, you'll add Tapestry information to each ZIP Code. <a href=\"https://doc.arcgis.com/en/esri-demographics/latest/regional-data/tapestry-segmentation.htm\">Tapestry</a> is a summary of many demographic and socioeconomic variables, including age groups and lifestyle choices. It'll teach you more about the types of people who live in your area of interest and help you better understand the reasons why growth happened where it did.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **Note:** Organizations should review the [data attributions](https://www.esri.com/en-us/legal/terms/data-attributions) and [Master Agreement](https://www.esri.com/content/dam/esrisites/en-us/media/legal/ma-full/ma-full.pdf) to make sure they are in compliance when geoenriching data and making it available to other systems."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The analysis variable **TSEGNAME** is 2022 Dominant Tapestry Segment Name such as Exurbanites, City Lights, Metro Renters, etc. The detailed summary of various segments can be found in <a href=\"https://doc.arcgis.com/en/esri-demographics/latest/regional-data/tapestry-segmentation.htm#ESRI_SECTION1_209EDB8ACF264A4785CE8D7C9210949D:~:text=Segmentation%20Methodology%20Statement-,Tapestry%20segment%20summaries,-The%2067%20distinct\">here</a>.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "{\"cost\": -1}\n"
     ]
    }
   ],
   "source": [
    "zip_enriched = enrich_layer(\n",
    "    permits_agg_layer,\n",
    "    analysis_variables=[\"AtRisk.TSEGNAME\"],\n",
    "    output_name=f\"zip_enriched_{dt.now().strftime('%Y%m%d%H%M%S')}\"\n",
    ")\n",
    "zip_enriched_layer = zip_enriched.layers[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "zip_enriched_sdf = pd.DataFrame.spatial.from_layer(zip_enriched_layer)\n",
    "zip_enriched_sdf.columns = [x if x == \"SHAPE\" else x.lower()\n",
    "                            for x in zip_enriched_sdf.columns]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>objectid</th>\n",
       "      <th>zip_code</th>\n",
       "      <th>po_name</th>\n",
       "      <th>state</th>\n",
       "      <th>population</th>\n",
       "      <th>pop_sqmi</th>\n",
       "      <th>sqmi</th>\n",
       "      <th>point_count</th>\n",
       "      <th>analysisarea</th>\n",
       "      <th>id</th>\n",
       "      <th>sourcecountry</th>\n",
       "      <th>enrich_fid</th>\n",
       "      <th>aggregationmethod</th>\n",
       "      <th>populationtopolygonsizerating</th>\n",
       "      <th>apportionmentconfidence</th>\n",
       "      <th>hasdata</th>\n",
       "      <th>tsegname</th>\n",
       "      <th>SHAPE</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>20012</td>\n",
       "      <td>Washington</td>\n",
       "      <td>DC</td>\n",
       "      <td>16069</td>\n",
       "      <td>6808.9</td>\n",
       "      <td>2.36</td>\n",
       "      <td>6</td>\n",
       "      <td>2.356958</td>\n",
       "      <td>0</td>\n",
       "      <td>US</td>\n",
       "      <td>1</td>\n",
       "      <td>BlockApportionment:US.BlockGroups;PointsLayer:...</td>\n",
       "      <td>2.191</td>\n",
       "      <td>2.576</td>\n",
       "      <td>1</td>\n",
       "      <td>City Lights</td>\n",
       "      <td>{\"rings\": [[[-8577435.6133, 4719746.3472], [-8...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>20783</td>\n",
       "      <td>Hyattsville</td>\n",
       "      <td>MD</td>\n",
       "      <td>50387</td>\n",
       "      <td>8061.92</td>\n",
       "      <td>6.25</td>\n",
       "      <td>1</td>\n",
       "      <td>6.252594</td>\n",
       "      <td>1</td>\n",
       "      <td>US</td>\n",
       "      <td>2</td>\n",
       "      <td>BlockApportionment:US.BlockGroups;PointsLayer:...</td>\n",
       "      <td>2.191</td>\n",
       "      <td>2.576</td>\n",
       "      <td>1</td>\n",
       "      <td>NeWest Residents</td>\n",
       "      <td>{\"rings\": [[[-8571874.5257, 4716758.1504], [-8...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>20814</td>\n",
       "      <td>Bethesda</td>\n",
       "      <td>MD</td>\n",
       "      <td>33669</td>\n",
       "      <td>6588.85</td>\n",
       "      <td>5.11</td>\n",
       "      <td>1143</td>\n",
       "      <td>5.109396</td>\n",
       "      <td>2</td>\n",
       "      <td>US</td>\n",
       "      <td>3</td>\n",
       "      <td>BlockApportionment:US.BlockGroups;PointsLayer:...</td>\n",
       "      <td>2.191</td>\n",
       "      <td>2.576</td>\n",
       "      <td>1</td>\n",
       "      <td>Metro Renters</td>\n",
       "      <td>{\"rings\": [[[-8586356.312, 4725395.7459], [-85...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>20815</td>\n",
       "      <td>Chevy Chase</td>\n",
       "      <td>MD</td>\n",
       "      <td>30893</td>\n",
       "      <td>5774.39</td>\n",
       "      <td>5.35</td>\n",
       "      <td>584</td>\n",
       "      <td>5.354012</td>\n",
       "      <td>3</td>\n",
       "      <td>US</td>\n",
       "      <td>4</td>\n",
       "      <td>BlockApportionment:US.BlockGroups;PointsLayer:...</td>\n",
       "      <td>2.191</td>\n",
       "      <td>2.576</td>\n",
       "      <td>1</td>\n",
       "      <td>Top Tier</td>\n",
       "      <td>{\"rings\": [[[-8584098.686, 4717919.8001], [-85...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>20816</td>\n",
       "      <td>Bethesda</td>\n",
       "      <td>MD</td>\n",
       "      <td>16512</td>\n",
       "      <td>3581.78</td>\n",
       "      <td>4.61</td>\n",
       "      <td>155</td>\n",
       "      <td>4.608462</td>\n",
       "      <td>4</td>\n",
       "      <td>US</td>\n",
       "      <td>5</td>\n",
       "      <td>BlockApportionment:US.BlockGroups;PointsLayer:...</td>\n",
       "      <td>2.191</td>\n",
       "      <td>2.576</td>\n",
       "      <td>1</td>\n",
       "      <td>Top Tier</td>\n",
       "      <td>{\"rings\": [[[-8591590.8551, 4717142.6318], [-8...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   objectid zip_code      po_name state  population  pop_sqmi  sqmi  \\\n",
       "0         1    20012   Washington    DC       16069    6808.9  2.36   \n",
       "1         2    20783  Hyattsville    MD       50387   8061.92  6.25   \n",
       "2         3    20814     Bethesda    MD       33669   6588.85  5.11   \n",
       "3         4    20815  Chevy Chase    MD       30893   5774.39  5.35   \n",
       "4         5    20816     Bethesda    MD       16512   3581.78  4.61   \n",
       "\n",
       "   point_count  analysisarea id sourcecountry  enrich_fid  \\\n",
       "0            6      2.356958  0            US           1   \n",
       "1            1      6.252594  1            US           2   \n",
       "2         1143      5.109396  2            US           3   \n",
       "3          584      5.354012  3            US           4   \n",
       "4          155      4.608462  4            US           5   \n",
       "\n",
       "                                   aggregationmethod  \\\n",
       "0  BlockApportionment:US.BlockGroups;PointsLayer:...   \n",
       "1  BlockApportionment:US.BlockGroups;PointsLayer:...   \n",
       "2  BlockApportionment:US.BlockGroups;PointsLayer:...   \n",
       "3  BlockApportionment:US.BlockGroups;PointsLayer:...   \n",
       "4  BlockApportionment:US.BlockGroups;PointsLayer:...   \n",
       "\n",
       "   populationtopolygonsizerating  apportionmentconfidence  hasdata  \\\n",
       "0                          2.191                    2.576        1   \n",
       "1                          2.191                    2.576        1   \n",
       "2                          2.191                    2.576        1   \n",
       "3                          2.191                    2.576        1   \n",
       "4                          2.191                    2.576        1   \n",
       "\n",
       "           tsegname                                              SHAPE  \n",
       "0       City Lights  {\"rings\": [[[-8577435.6133, 4719746.3472], [-8...  \n",
       "1  NeWest Residents  {\"rings\": [[[-8571874.5257, 4716758.1504], [-8...  \n",
       "2     Metro Renters  {\"rings\": [[[-8586356.312, 4725395.7459], [-85...  \n",
       "3          Top Tier  {\"rings\": [[[-8584098.686, 4717919.8001], [-85...  \n",
       "4          Top Tier  {\"rings\": [[[-8591590.8551, 4717142.6318], [-8...  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zip_enriched_sdf.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"\" alt=\"Image\" />"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "map_enriched = gis.map(\"Montgomery County, Maryland\")\n",
    "map_enriched"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "map_enriched.content.add(zip_enriched_layer)  # visualizing boundaries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "map_enriched.content.add(zip_enriched_layer)  # visualizing population"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "zip_enriched_layer_sm = map_enriched.content.renderer(1).smart_mapping()\n",
    "zip_enriched_layer_sm.class_breaks_renderer(\n",
    "    break_type=\"size\",\n",
    "    field=\"population\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Click some of the ZIP Codes.\n",
    "\n",
    "The Tapestry segment is displayed when you click a ZIP Code. The Tapestry segments have names such as Enterprising Professionals and Savvy Suburbanites. You can look up more information about each segment, including its specific demographic characteristics, on the<a href=\"http://doc.arcgis.com/en/esri-demographics/data/tapestry-segmentation.htm\"> Tapestry Segmentation help page</a>.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What Tapestry segment is dominant for the Clarksburg ZIP Code where major growth occurred? Click the Clarksburg ZIP Code to find out. According to the pop-up, Boomburbs is the dominant Tapestry segment for the ZIP Code. Boomburbs have many young professionals with families living in affordable new housing. This description may explain why the area saw such rapid residential growth in 2011. It's possible that other ZIP Codes with similar demographic profiles may experience rapid growth in the near future.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Click the ZIP Code directly southwest of Clarksburg.\n",
    "\n",
    "This ZIP Code is in Boyds. It also has the Boomburbs Tapestry segment. However, its number of permits has been relatively low since 2010. The county may be able to anticipate a similar spike in permit activity in this area.\n",
    "\n",
    "Although Tapestry segments are based on several demographic characteristics, you could also perform this analysis with other variables. For instance, you could determine if there is a correlation between high permit activity and high population growth. Is a young population or a high income level a stronger indicator of growth? You can answer these questions and others with the analysis tools at your disposal. For the purposes of this lesson, however, your results are satisfactory.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Share your work\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We've analyzed your data and come to a couple conclusions about your data. Next, we'll share your results online. Currently, our result layers are layers that are accessible only to you. Sharing our data will make it easier for county officials to use your data in other ArcGIS applications and communicate key information to the public. In particular, we'll share your work to ArcGIS Online. We'll share your enriched ZIP Codes dataset as feature layers that can be added to any web map.\n",
    "\n",
    "The layer contains fields for both the count of permits per ZIP Code and the dominant Tapestry segment—basically all of the result data we created in your analysis. We'll only need to share this layer, not the original aggregation layer.\n",
    "\n",
    "Using the `sharing_level` property, you can share your work with others.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "zip_enriched.sharing.sharing_level = \"EVERYONE\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this notebook, we used ArcGIS API for Python to explore and analyze permit data for Montgomery County, Maryland. You answered questions about your data's spatial and temporal trends and located areas of the county with rapid growth. We compared your findings with demographic data, came to conclusions about the possible causes of growth, and even predicted an area that may experience similar growth in the future based on shared demographic characteristics. With ArcGIS API for Python, we can perform a similar workflow on any of your data to better understand what it contains and what questions it can answer.\n"
   ]
  }
 ],
 "metadata": {
  "esriNotebookRuntime": {
   "notebookRuntimeName": "ArcGIS Notebook Python 3 Standard",
   "notebookRuntimeVersion": "9.0"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": true,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
